package com.zhht.checkcop.demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import com.zhht.checkcop.db.MysqlDb;

public class UpdateNovelErrorName implements PageProcessor{
	
	private Site site = Site.me().setRetryTimes(3).setSleepTime(1500);
	public UpdateNovelErrorName(){}
	private List<Map<String,Object>> listMap;
	private static Map<String,Map<String,Object>> staticMap;
	public UpdateNovelErrorName(List<Map<String,Object>> listMap){
		this.listMap = listMap;
	}
	private Boolean bool=true;
    public void process(Page page) {
    	if(bool){
    		staticMap =new HashMap<String, Map<String,Object>>();
    		List<String> listStr=new ArrayList<String>();
    		for(Map<String,Object> map:listMap){
    			listStr.add(map.get("source_url")+"");
    			staticMap.put(map.get("source_url")+"", map);
    		}
    		page.addTargetRequests(listStr);
    		bool=false;
    	}else{
    		String name = page.getHtml().xpath("//div[@class='rt']/h1/text()").get();
	    	
    		String url=page.getRequest().getUrl();
    		if(staticMap.get(url)!=null){
    			MysqlDb mysqlDb=null;
    			try{
    				mysqlDb=new MysqlDb();
    				mysqlDb.connect();
    				System.out.println("update novel_info set name = '"+name+"' where id = "+staticMap.get(url).get("id"));
    				mysqlDb.execute("update novel_info set name = '"+name+"' where id = "+staticMap.get(url).get("id"));
    				
    			}catch(Exception e){
    				e.printStackTrace();
    			}finally{
    				try {
						mysqlDb.close();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
    			}
    		}
    	}
    }

    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
    	int thread = args!=null&&args.length>0?Integer.parseInt(args[0]):1;
    	try{
    		MysqlDb mysqlDb=new MysqlDb();
    		mysqlDb.connect();
    		
    		List<Map<String,Object>> listMap=mysqlDb.query("select ni.id,ni.name,ni.author_name,ni.source_url from novel_info ni INNER JOIN (select name,author_name from novel_info GROUP BY name,author_name HAVING count(1) >1) aa on ni.`name` = aa.`name` AND ni.author_name = aa.author_name ORDER BY `name`");
    		
    		Spider.create(new UpdateNovelErrorName(listMap)).addUrl("https://www.88dus.com/").thread(thread).run();
    		
    		mysqlDb.close();
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	
    }
}
